import { ref } from 'vue';
import { Message } from '@arco-design/web-vue';
import { useFilmReadRight, useFilmReadLeft } from '@/store';
import { delLesionData } from '@/api/readPathology/readPathology';
import type * as Type from '@/store/modules/filmReadLeft/type';

export default function usePageLeft (provideState: { getLesionItem: Function }) {
  const FilmReadRight = useFilmReadRight();
  const FilmReadLeft = useFilmReadLeft();
  // 左侧眼睛属性值
  const eyeAllVisibile = ref(true);

  // 内容区左侧列表内容标题显示值
  const submenuVisibile = ref(true);

  // 内容区左侧列表item显示值
  const menuItemVisibile = ref(true);

  // 左侧列表显示全部显示隐藏事件
  const handleAllEyeClick = (flag: boolean, e: { stopPropagation: () => void; }) => {
    e.stopPropagation();
    eyeAllVisibile.value = !flag;
    menuItemVisibile.value = !flag;
    provideState.getLesionItem(() => {
      return {
        currentItem: FilmReadLeft.isShowAllLesion(),
        type: 'all',
        isShowAll: eyeAllVisibile.value
      };
    });
  }

  // 内容区左侧列表内容标题点击(下拉切换)
  const handleSubmenuClick = (visibile: boolean) => {
    FilmReadRight.currentSubmenuItem = '';
    submenuVisibile.value = !visibile;
    menuItemVisibile.value = !visibile;
  }

  // 左侧列表当前值点击
  const handleSubmenuItemClick = (item: Type.NidusList) => {
    if (FilmReadRight.currentSubmenuItem !== String(item.disease.id)) {
      FilmReadRight.currentSubmenuItem = String(item.disease.id);
      FilmReadRight.currentMiniSeriesSet = item.disease.seriesList;
    } else {
      FilmReadRight.currentSubmenuItem = '';
    };
    provideState.getLesionItem(() => {
      return {
        currentItem: FilmReadLeft.setCurrentLesion(item.disease.id),
        type: 'lesion',
        isLesion: FilmReadRight.currentSubmenuItem,
        id: +item.disease.id,
      };
    });
  }

  // 左侧item眼睛事件
  const handleEyeClick = (item: Type.NidusList, e: { stopPropagation: () => void; }) => {
    e.stopPropagation();
    item.disease.eyeStatus = !item.disease.eyeStatus;
    provideState.getLesionItem(() => {
      return {
        currentItem: FilmReadLeft.setCurrentLesion(item.disease.id),
        type: 'eyes',
        id: +item.disease.id,
        isEyes: item.disease.eyeStatus
      };
    });
  }

  // 左侧item颜色变更事件
  const handleColorChange = async (item: Type.NidusList, value: string) => {
    provideState.getLesionItem(() => {
      return {
        currentItem: FilmReadLeft.setCurrentLesion(item.disease.id),
        value,
        type: 'color'
      };
    });
  }

  // 左侧item删除事件
  const handleDelClick = async (item: Type.NidusList, e: { stopPropagation: () => void; }) => {
    e.stopPropagation();
    const alrId = item.disease.id;
    const delItem = FilmReadLeft.setCurrentLesion(item.disease.id)
    // 病灶中移除当前项
    FilmReadLeft.showLesionList = FilmReadLeft.showLesionList.filter((item: { disease: { id: number; }; }) => item.disease.id !== alrId);
    provideState.getLesionItem(() => {
      if (!FilmReadLeft.showLesionList.length) {
        FilmReadRight.currentSubmenuItem = ''
      }
      return {
        currentItem: delItem,
        id: alrId,
        type: 'del'
      };
    });
    const res = await delLesionData(String(alrId));
    Message.success('删除成功！');
  }
  return {
    eyeAllVisibile,
    submenuVisibile,
    menuItemVisibile,
    handleAllEyeClick,
    handleSubmenuClick,
    handleSubmenuItemClick,
    handleEyeClick,
    handleDelClick,
    handleColorChange
  }
}